package jun.learn.javalinux;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class ExecLinuxComand {
	public static void main(String[] args) throws Exception {
		String hostName = "192.168.17.152";
		String username = "barcode";
		String password = "deppontest";
		
		Connection conn =new Connection(hostName);
		Session ssh = null;
		conn.connect();
		
		boolean isAuthPass = conn.authenticateWithPassword(username, password);
	
		if(!isAuthPass){
			System.out.println("用户名或者密码错误");
		} else{
			 ssh = conn.openSession();
			 ssh.execCommand("ps -ef");
			 InputStream is = new StreamGobbler(ssh.getStdout());
			 
			 BufferedReader br = new BufferedReader(new InputStreamReader(is));
			 while(true){
				 String line = br.readLine();
				 if(line == null){
					 break;
				 }
				 System.out.println(line);
			 }
		}
		ssh.close();
		conn.close();
	}
}
